local skel = fk.CreateSkill {
  name = "emo__jingying",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__jingying"] = "菁英",
  [":emo__jingying"] = "锁定技，若你本轮受到或造成过伤害，你的弃牌阶段改为出牌阶段。你于你出牌阶段内使用的【杀】结算结束后，你摸X张牌，然后结束当前阶段（X为你于本阶段使用牌的类型数）。",

  ["#PhaseChanged"] = "%from 的 %arg 被改为了 %arg2",
  ["#emo__jingying_endplay"] = "由于 %arg 的效果，%from 结束出牌阶段",

  ["$emo__jingying1"] = "这才是精准无误的行动。",
  ["$emo__jingying2"] = "今天的我，也不会输给任何人的。",
}

local EMO = require "packages.moepack.utility.emo_util"

skel:addEffect(fk.EventPhaseChanging, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      return data.phase == Player.Discard and #player.room.logic:getActualDamageEvents(1, function(e)
        return e.data.from == player or e.data.to == player
      end, Player.HistoryRound) > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.phase = Player.Play
    room:sendLog{
      type = "#PhaseChanged",
      from = player.id,
      arg = "phase_discard",
      arg2 = "phase_play",
    }
  end,
})

skel:addEffect(fk.CardUseFinished, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      return player.phase == Player.Play and data.card.trueName == "slash"
      and player:usedEffectTimes(self.name, Player.HistoryPhase) == 0 -- 防止多次触发
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:endPlayPhase() -- 有时没用，不知何意
    local types = {}
    room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      local use = e.data
      if use.from == player then
        table.insertIfNeed(types, use.card.type)
      end
      return #types == 3
    end, Player.HistoryPhase)
    player:drawCards(#types, skel.name)
    room:sendLog{ type = "#emo__jingying_endplay", from = player.id, arg = skel.name }
    -- 这样总能结束阶段了吧？
    local phase_event = room.logic:getCurrentEvent():findParent(GameEvent.Phase)
    if phase_event then
      phase_event.data.phase_end = true
    end
    room:delay(1500) -- 为了语音不重叠哈
  end,
})

-- 以下为切换皮肤

skel:addEffect(fk.Damaged, {
  can_refresh = function (self, event, target, player, data)
    return (player.general == "moesp__hkrifle" or player.deputyGeneral == "moesp__hkrifle") and (data.from == player or data.to == player)
  end,
  on_refresh = function (self, event, target, player, data)
    EMO.changeSkin(player, "moesp__hkrifle", "moesp__hkrifle2")
  end,
})

skel:addEffect(fk.RoundEnd, {
  can_refresh = function (self, event, target, player, data)
    return player.general == "moesp__hkrifle2" or player.deputyGeneral == "moesp__hkrifle2"
  end,
  on_refresh = function (self, event, target, player, data)
    EMO.changeSkin(player, "moesp__hkrifle2", "moesp__hkrifle")
  end,
})

return skel
